import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_hive_dao.dart';
import '../../../dd_core/dd_http.dart';
import '../../../dd_core/dd_log_service.dart';
import '../../../dd_models/dd_hive_works_model.dart';

class DDHomeSecondController extends GetxController {
  final list = <HiveWorksModel>[].obs;

  @override
  onInit() {
    super.onInit();
    getList();
  }

  getList() async {
    final prefs = await SharedPreferences.getInstance();
    String cureenUid = await prefs.getString(DDKey.cureenUid) ?? "";
    List<HiveWorksModel>? d =
        await DDHiveDao.getFencingList(uid: cureenUid, type: 1);
    if (d != null) {
      list.value = d;
      sortListByFencingIdDesc(list);
      list.refresh();
    }
  }

  void sortListByFencingIdDesc(RxList<HiveWorksModel> list) {
    list.sort((a, b) => b.fencingid!.compareTo(a.fencingid!));
  }

  @override
  void onClose() {
    list.close();
    super.onClose();
  }

  updateMyLike(HiveWorksModel m) async {
    await DDHiveDao.updateFencingLikes(hiveWorksModel: m);
    getList();
  }

  updateFollow(HiveWorksModel m) async {
    bool myFollow = true;
    if (m.myFollow != null) {
      myFollow = !m.myFollow!;
    }
    if (myFollow) {
      EasyLoading.show();
      Map<String, dynamic>? result = await addFriend(m.userId ?? '');
      EasyLoading.dismiss();
      if (result != null && result['code'] == 0) {
        await DDHiveDao.updateFencingFollow(
            hiveWorksModel: m, myFollow: myFollow);
        await getList();
        if (myFollow) {
          EasyLoading.showToast("Follow Successfully",
              duration: Duration(seconds: 2));
        }
      }
    } else {
      EasyLoading.show();
      Map<String, dynamic>? result = await unfriend(m.userId ?? '');
      EasyLoading.dismiss();
      if (result != null && result['code'] == 0) {
        await DDHiveDao.updateFencingFollow(
            hiveWorksModel: m, myFollow: myFollow);
        await getList();
        if (!myFollow) {
          EasyLoading.showToast("Unfollow Successfully",
              duration: Duration(seconds: 2));
        }
      }
    }
  }

  updateMyBlock(HiveWorksModel m) async {
    DDLogService().ddGa(page: "block");

    bool myBlock = true;
    if (m.myBlock != null) {
      myBlock = !m.myBlock!;
    }
    if (myBlock) {
      EasyLoading.show();
      var p = {
        "broadcasterId": m.userId ?? '',
        "complainCategory": 'Block',
      };
      Map<String, dynamic>? result = await insertRecord(p);
      EasyLoading.dismiss();
      if (result != null && result['code'] == 0) {
        await DDHiveDao.updateFencingBlock(hiveWorksModel: m, myBlock: myBlock);
        getList();
      }
    } else {
      EasyLoading.show();
      Map<String, dynamic>? result = await removeBlock(m.userId ?? '');
      EasyLoading.dismiss();
      if (result != null && result['code'] == 0) {
        await DDHiveDao.updateFencingBlock(hiveWorksModel: m, myBlock: myBlock);
        getList();
      }
    }
  }

  report(HiveWorksModel m, String content) async {

    DDLogService().ddGa(page: "report");

    EasyLoading.show();
    var p = {
      "broadcasterId": m.userId ?? '',
      "complainCategory": 'Report',
      "complainSub": content,
    };
    Map<String, dynamic>? result = await insertRecord(p);
    EasyLoading.dismiss();
    if (result != null && result['code'] == 0) {
      EasyLoading.showToast("Report Successfully",
          duration: Duration(seconds: 2));
    }
  }
}
